package com.fruitbasket.audioplatform;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;
import androidx.preference.EditTextPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;

public class SettingFragment extends PreferenceFragmentCompat  {

    SharedPreferences sp_settingFragment = null;
    SharedPreferences sp_userData = null;
    Context context = ContextUtil.getContext();
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.preference_settings, rootKey);

        //add prefrence provider  need : implementation 'androidx.preference:preference:1.1.1'
        sp_settingFragment = getPreferenceManager().getSharedPreferences();
        EditTextPreference operatorNamePreference = (EditTextPreference) findPreference("report_operator_name");
        EditTextPreference projectNamePreference = (EditTextPreference) findPreference("report_project_name");
        EditTextPreference projectNoPreference = (EditTextPreference) findPreference("report_project_num");

        initPrefrence( operatorNamePreference, "report_operator_name", "defaultVal" );
        initPrefrence( projectNamePreference, "report_project_name", "defaultVal" );
        initPrefrence( projectNoPreference, "report_project_num", "defaultVal" );
//        if (operatorNamePreference != null) {
//            operatorNamePreference.setSummaryProvider(EditTextPreference.SimpleSummaryProvider.getInstance());
//            operatorNamePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
//                @Override
//                public boolean onPreferenceChange(Preference preference, Object newValue) {
//                    if(sp_settingFragment != null){
//                        sp_settingFragment = preference.getSharedPreferences();
//                        String tmp = sp_settingFragment.getString("report_operator_name","report_operator_name");
//                        SharedPreferences.Editor editor = sp_userData.edit();
//                        editor.putString("report_operator_name", tmp);
//                        Log.i(ContextUtil.LOG_TAG, "settingFragmnet "+tmp);
//                        editor.commit();
//                    }
//
//                    return true;
//                }
//            });
//        }
//        if (projectNamePreference != null) {
//            projectNamePreference.setSummaryProvider(EditTextPreference.SimpleSummaryProvider.getInstance());
//            projectNamePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
//                @Override
//                public boolean onPreferenceChange(Preference preference, Object newValue) {
//                    if(sp_settingFragment != null){
//                        sp_settingFragment = preference.getSharedPreferences();
//                        String tmp = sp_settingFragment.getString("report_project_name","report_project_name");
//                        SharedPreferences.Editor editor = sp_userData.edit();
//                        editor.putString("report_project_name", tmp);
//                        Log.i(ContextUtil.LOG_TAG, "settingFragmnet "+tmp);
//                        editor.commit();
//                    }
//
//                    return true;
//                }
//            });
//        }
//        if (projectNoPreference != null) {
//            projectNoPreference.setSummaryProvider(EditTextPreference.SimpleSummaryProvider.getInstance());
//        }

    }

    private void initPrefrence(Preference pre, String key, String defaultVal){
        if (pre != null) {

            pre.setSummaryProvider(EditTextPreference.SimpleSummaryProvider.getInstance());
            pre.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {

                    if(sp_settingFragment != null){
                        Log.i(ContextUtil.LOG_TAG, "settingFragmnet initPrefrence newValue......"+newValue.toString());

                        //String tmp = sp_settingFragment.getString(key,defaultVal);
                        sp_userData = context.getSharedPreferences("userData",Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sp_userData.edit();
                        editor.putString( key, newValue.toString() );
                        editor.commit();

                        Log.i( ContextUtil.LOG_TAG, "settingFragmnet initPrefrence getkey::" + sp_userData.getString(key, "xxxxxxx") );
                        Log.i( ContextUtil.LOG_TAG, "settingFragmnet initPrefrence summury......"+preference.getSummary() );
                    }

                    return true;
                }
            });
        }
    }


    public boolean onPreferenceTreeClick (Preference preference){


//        switch(preference.getKey()){
//            case "report_operator_name":
//                //String summary = (String) preference.getSummary();
//                String name  = sp_settingFragment.getString("report_operator_name", "defaultName");
//                Toast.makeText(context,name, Toast.LENGTH_LONG).show();
//                //preference.setSummary( name );
//                break; //
//            case "report_project_name" :
//                String projectName  = sp_settingFragment.getString("report_project_name", "defaultName");
//                Toast.makeText(context,projectName , Toast.LENGTH_LONG).show();
//                break;
//            default :
//                Toast.makeText(context,"noting todo", Toast.LENGTH_LONG).show();
//        }


        return true;
    }
//    @Override
//    public View onCreateView(
//            LayoutInflater inflater, ViewGroup container,
//            Bundle savedInstanceState
//    ) {
//        // Inflate the layout for this fragment
//        return inflater.inflate(R.layout.fragment_setting, container, false);
//    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


//        view.findViewById(R.id.button_toFirst).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                NavHostFragment.findNavController(SettingFragment.this)
//                        .navigate(R.id.action_SettingFragment_to_FirstFragment);
//            }
//        });

//        view.findViewById(R.id.button_toSecond).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                NavHostFragment.findNavController(SettingFragment.this)
//                        .navigate(R.id.action_FirstFragment_to_SecondFragment);
//            }
//        });
    }


}